<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>vue组件</title>
</head>

<body>
  <div id="app">
    <!-- 使用组件的时候传递真实参数 -->
    <child name="小明" age="18" @response="(msg)=> child1 = msg"></child>
    <p>{{child1}}</p>

    <child name="小李" age="20" @response="(msg)=> child2 = msg"></child>
    <p>{{child2}}</p>

  </div>
  <template id="child">
    <div>
      <h1>你好鸭，我是{{name}},我今年{{age}}岁了</h1>
      <p>{{count}}</p>
      <button @click="count++">++++</button>
      <button @click="send">看我儿子发了啥</button>
    </div>
  </template>
  <script src="https://cdn.jsdelivr.net/npm/vue@2.7.8/dist/vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      components: {
        child: {
          template: '#child',
          // 注册组件的时候定义参数名字,用来接收父组件给的状态
          props: ['name', 'age'],
          // 组件自己独立的状态
          data() {
            return {
              count: 0,
            }
          },
          computed: {},
          methods: {
            send() {
              // 在子组件中向父组件传递信息的方式=》 触发父组件中的自定义事件
              this.$emit('response', this.name)
            }
          }
        }
      },
      data: {
        // count: 0
        child1: '',
        child2: '',
      },
      methods: {
        // 接收子组件传递的信息
        fn(message) {
          this.child1 = message
        }
      }
    });
  </script>
</body>

</html>